﻿using MainScene;
using MainScene.Evt.Command;
using MainScene.Evt.Condition;
using NodeEditorFramework;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class LevelMapSubCanvas : NodeEditorFramework.NodeSubCanvas
{
    private StoryNode curDrawNode;
    private Rect SubRect;

    private Vector2 nodeCanvasLastPanPos;
    private Rect nodeCanvasRectNoScale;
    private NodeEditorState cacheState;
    /// <summary>
    /// NodeEditorState 缩放计算前调用
    /// </summary>
    /// <param name="nodeEditorState"></param>

    public override void SetNodeEditorState(NodeEditorState nodeEditorState)
    {
        cacheState = nodeEditorState;
        nodeCanvasLastPanPos = nodeEditorState.panOffset;
        nodeCanvasRectNoScale = nodeEditorState.canvasRect;
    }
    public override void DrawSubCanvas()
    {
        var temp = NodeEditor.curEditorState.selectedNode as StoryNode;
        if ( (curDrawNode != null && temp == null) || (curDrawNode != null && temp != curDrawNode)) //当前交点丢失的时候对节点的操作重置
        {
            curDrawNode.ResetOption();
        }
        curDrawNode = temp;
        if (curDrawNode == null || !curDrawNode.NeedDrawSubPanel())
        {
            //TODO 直接加入 Group 的input 会有异常
            //cacheState.ignoreInput.Remove(SubRect);
            SubRect = Rect.zero;
            return;
        }

        Rect canvasRect = nodeCanvasRectNoScale;

        #region TODO拖拽
        Vector2 panPos = nodeCanvasLastPanPos;
        var maxX = canvasRect.width - LevelMapSubEditor.PanelWidthFloat;
        float x = maxX;
        float y = 20;
        if (x < 0)
        {
            x = 0;
        }
        else if (x > maxX)
        {
            x = maxX;
        }
        if (y < 20)
        {
            y = 20;
        }
        else if (y > LevelMapSubEditor.PanelHeightFloat)
        {
            y = LevelMapSubEditor.PanelHeightFloat;
        }
        #endregion

        Rect nodeRect = new Rect(x, y, LevelMapSubEditor.PanelWidthFloat, LevelMapSubEditor.PanelHeightFloat);

        Vector2 contentOffset = new Vector2(0, 20);
        GUI.color = Color.white;

        // Create a headerRect out of the previous rect and draw it, marking the selected node as such by making the header bold
        Rect headerRect = new Rect(nodeRect.x, nodeRect.y, nodeRect.width, contentOffset.y);
        GUI.color = Color.white;
        GUI.Box(headerRect, GUIContent.none, GUI.skin.box);
        GUI.color = Color.white;
        GUI.Label(headerRect, "SubCanvas", NodeEditorGUI.nodeLabelBoldCentered);

        // Begin the body frame around the NodeGUI
        Rect bodyRect = new Rect(nodeRect.x, nodeRect.y + contentOffset.y, nodeRect.width, nodeRect.height - contentOffset.y);
        GUI.color = Color.white;
        GUI.BeginGroup(bodyRect, GUI.skin.box);
        GUI.color = Color.white;
        bodyRect.position = Vector2.zero;
        GUILayout.BeginArea(bodyRect);

        // Call NodeGUI
        GUI.changed = false;

        DrawCellEditor();
        DrawCondtionEditor();
        DrawCreateCellPanel();
        DrawCreateConditionPanel();

        // End NodeGUI frame
        GUILayout.EndArea();
        GUI.EndGroup();

        //每次绘制时候 对子窗口事件输入忽略 
        SubRect = new Rect(nodeRect.x, nodeRect.y + contentOffset.y, nodeRect.width, nodeRect.height + contentOffset.y);
        cacheState.ignoreInput.Add(SubRect);
    }
    Vector2 scroll_condition_list_pos = Vector2.zero;
    /// <summary>
    /// 绘制条件
    /// </summary>
    private void DrawCreateConditionPanel()
    {
        if (curDrawNode.isEditorNewCondition == false)
            return;
        GUILayout.BeginVertical("box");
        {
            GUILayout.BeginHorizontal("box");
            {
               
                GUILayout.Label("New Story Condition");
                if (GUILayout.Button("Add New Condition") && curDrawNode.new_story_condtion != null)
                {
                    curDrawNode.AddNewCondtion(curDrawNode.new_story_condtion);
                    curDrawNode.isEditorNewCondition = false;
                    curDrawNode.new_story_condtion = null;
                }
            }
            GUILayout.EndHorizontal();

            curDrawNode.new_condition_type = (EvtConditionType)EditorGUILayout.EnumPopup("EvtConditionType", curDrawNode.new_condition_type, LevelMapSubEditor.PanelWidth);

            if (null == curDrawNode.new_story_condtion || curDrawNode.new_condition_type != curDrawNode.new_story_condtion.conditionType)
            {
                curDrawNode.new_story_condtion = LevelMapSubEditor.CreateSceneEvtCondition(curDrawNode.new_condition_type);
            }

            scroll_condition_list_pos = GUILayout.BeginScrollView(scroll_condition_list_pos);
            LevelMapSubEditor.EditorData(curDrawNode.new_story_condtion.GetType(), curDrawNode.new_story_condtion, LevelMapSubEditor.PanelWidth, LevelMapSubEditor.LableWidth);
            GUILayout.EndScrollView();
        }
        GUILayout.EndVertical();
    }

    private Vector2 scroll_list_pos = Vector2.zero;
    private void DrawCreateCellPanel()
    {
        if (curDrawNode.isEditorNewCell == false)
            return;
        GUILayout.BeginVertical("box", LevelMapSubEditor.PanelWidth);
        {
            GUILayout.BeginHorizontal("box");
            {
                GUILayout.Label("New Story Cell:");
                if (GUILayout.Button("Add New Cell") && curDrawNode.new_story_command != null)
                {
                    curDrawNode.AddNewCommand(curDrawNode.new_story_command);
                    curDrawNode.isEditorNewCell = false;
                    curDrawNode.new_story_command = null;
                }
            }
            GUILayout.EndHorizontal();

            curDrawNode.new_command_type = (EvtCommandType)EditorGUILayout.EnumPopup("EvtCommandType", curDrawNode.new_command_type, LevelMapSubEditor.PanelWidth);
            if (null == curDrawNode.new_story_command || curDrawNode.new_story_command.commandType != curDrawNode.new_command_type)
            {
                curDrawNode.new_story_command = LevelMapSubEditor.CreateSceneEvtCommand(curDrawNode.new_command_type);
            }

            scroll_list_pos = GUILayout.BeginScrollView(scroll_list_pos, "box");
            LevelMapSubEditor.EditorData(curDrawNode.new_story_command.GetType(), curDrawNode.new_story_command, LevelMapSubEditor.PanelWidth, LevelMapSubEditor.LableWidth);
            GUILayout.EndScrollView();
        }
        GUILayout.EndVertical();
    }

    private void DrawCondtionEditor()
    {
        if (curDrawNode.EditorConditonIndex == -1)
            return;
        ConditionData story_condition = curDrawNode.GetCondition(curDrawNode.EditorConditonIndex);
        if (story_condition == null)
        {
            return;
        }
        GUILayout.BeginVertical("box", LevelMapSubEditor.PanelWidth);
        GUILayout.Label(string.Format("Editor Story Condition:{0}", story_condition.GetType().ToString()));

        scroll_list_pos = GUILayout.BeginScrollView(scroll_list_pos, "box");
        LevelMapSubEditor.EditorData(story_condition.GetType(), story_condition, LevelMapSubEditor.PanelWidth, LevelMapSubEditor.LableWidth);

        GUILayout.EndScrollView();
        GUILayout.EndVertical();
    }

    private void DrawCellEditor()
    {
        if (curDrawNode.EditorCommandIndex == -1)
            return;
        CommandData command_condition = curDrawNode.GetCell(curDrawNode.EditorCommandIndex);
        if (command_condition == null)
            return;
        GUILayout.BeginVertical("box", LevelMapSubEditor.PanelWidth);
        {
            GUILayout.Label(string.Format("EditorStoryCommand;{0}", command_condition.GetType().ToString()));
            scroll_list_pos = GUILayout.BeginScrollView(scroll_list_pos, "box");
            LevelMapSubEditor.EditorData(command_condition.GetType(), command_condition, LevelMapSubEditor.PanelWidth, LevelMapSubEditor.LableWidth);
            GUILayout.EndScrollView();
        }
        GUILayout.EndVertical();
    }
}
